package com.unsiv.torch;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * <p>
 * start activity
 * </p>
 * 
 * @author fanzhen
 * @time 7:31:13 PM Aug 26, 2011
 * @email fanzhen.meng@gmail.com
 */
public class TorchInit extends Activity {
	private boolean isFirstUse;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		View layout = new LinearLayout(this);
		layout.setBackgroundResource(R.drawable.init);
		setContentView(layout);
	}

	private void initData() {
		SharedPreferences setting = getSharedPreferences("setting", 0);
		SharedPreferences.Editor settingEditor = setting.edit();
		SharedPreferences screen_mode = getSharedPreferences("screen_mode", 0);
		SharedPreferences.Editor screen_mode_editor = screen_mode.edit();
		SharedPreferences led_mode = getSharedPreferences("led_mode", 0);
		SharedPreferences.Editor led_mode_editor = led_mode.edit();
		if (screen_mode.getAll().size() == 0) {
			screen_mode_editor.putString("1", "brightness,100");
			screen_mode_editor.putString("2", "brightness,50");
			screen_mode_editor.putString("3", "strobe,5");
			screen_mode_editor.putString("4", "strobe,15");
			screen_mode_editor.putString("5", "customize,1,1,2,1,2,1");
			screen_mode_editor.commit();
			settingEditor.putInt("current_led_mode", 1);
		}
		if (led_mode.getAll().size() == 0) {
			led_mode_editor.putString("1", "brightness,100");
			led_mode_editor.putString("2", "brightness,50");
			led_mode_editor.putString("3", "strobe,50");
			led_mode_editor.putString("4", "strobe,15");
			led_mode_editor.putString("5", "customize,1,1,2,1,2,1");
			led_mode_editor.commit();
			settingEditor.putInt("current_led_mode", 1);
		}
		isFirstUse = setting.getBoolean("isFirstUse", true);
		settingEditor.putBoolean("isFirstUse", false);
		settingEditor.commit();
	}

	@Override
	protected void onStart() {
		int flag = getIntent().getIntExtra("flag", 0);
		if (flag == ActivityUtil.EXIT_APPLICATION) {
			this.finish();
		}
		super.onStart();
	}

	@Override
	protected void onResume() {
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			public void run() {
				initData();
				boolean supportTorch = MyCamaraHelper.checkCamera();
				Intent intent;
				if (supportTorch) {
					if (isFirstUse) {
						intent = new Intent(getApplication(), Navigate.class);
						intent.putExtra("type", "led");
					} else {
						intent = new Intent(getApplication(), LedTorch.class);
					}
				} else {
					if (isFirstUse) {
						intent = new Intent(getApplication(), Navigate.class);
						intent.putExtra("type", "screen");
					} else {
						intent = new Intent(getApplication(), ScreenTorch.class);
					}
				}
				startActivity(intent);
				// TorchInit.this.finish();
			}
		}, 2000);
		super.onResume();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		int flag = getIntent().getIntExtra("flag", 0);
		if (flag == ActivityUtil.EXIT_APPLICATION) {
			this.finish();
		}
		super.onNewIntent(intent);
	}
}